/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.UserBean;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.servlet.http.HttpServletResponse;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import radio.entities.AccountDetails;
import radio.entities.Accounts;
import radio.sessions.AccountDetailsFacade;
import radio.sessions.AccountsFacade;

/**
 *
 * @author Kim
 */
public class LoginForm implements Serializable {

    @EJB
    private AccountsFacade accountsFacade;
    private AccountDetailsFacade details;

    /** Creates a new instance of LoginForm */
    public LoginForm() {
    }
    private String username;
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void Login() throws IOException {
//        HttpServletRequest rep=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();        
        if (username == null && password == null) {
            System.out.println("Loi");
        } else if (!username.equals("") && !password.equals("")) {

            Accounts acc = accountsFacade.login(this.username, this.password);
            if (acc != null) {
                AccountDetails detail = acc.getAccountDetails();
                UserBean us = new UserBean(acc.getUid(), acc.getPwd(), acc.getImg(), acc.getAccountDetails().getContactPerson(), acc.getAccountDetails().getMobile(), acc.getAccountDetails().getTelephone(), acc.getAccountDetails().getAddress(), acc.getAccountDetails().getEmail(), acc.isGender(), acc.getStatus(), acc.getAccId(), acc.getTypeId().getTypeId(), acc.getAccountDetails().getCityId().getCityId());
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("userBean", us);
                HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                ses.setAttribute("user", this.username);
                HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                res.sendRedirect("./../Admin/mainPage.xhtml");
            }
        } else {
            System.out.println("Loi");
        }
    }

    public void logout() throws IOException {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession ses = req.getSession();
        ses.removeAttribute("user");
        HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        res.sendRedirect("./../Main/MainPage.xhtml");
    }
}